package com.xingu.xg.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @param
 * @BelongsProject: xg-interview-java
 * @BelongsPackage: com.xingu.xg.config
 * @author: L
 * @Date: 2024/5/20 2:18
 * @return
 */
@Data
@Component
@ConfigurationProperties(prefix = "threadpool")
public class ThraedPoolConfig {
    private Integer coreSize;
    private Integer maxSize;
    private Long keepTime;


    @Bean
    public ScheduledThreadPoolExecutor scheduledThreadPoolExecutor(){
        return new ScheduledThreadPoolExecutor(
                coreSize,
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        return new ThreadPoolExecutor(coreSize,
                maxSize,
                keepTime,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
